package com.jingdianjichi.changerecord.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.*;
import cn.hutool.json.JSONUtil;
import com.jingdianjichi.changerecord.entity.ChangeRecord;
import com.jingdianjichi.changerecord.strategy.DictStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * 默认变更记录实现类，内存存储
 *
 * @author Hu_bx
 * @date 13:23 2023/9/16
 **/
@Slf4j
public class DefaultChangeRecordImpl extends AbstractChangeRecordImpl {

    private final Map<String, ChangeRecord> changeRecordMap = MapUtil.newHashMap();

    public DefaultChangeRecordImpl(DictStrategy dictStrategy) {
        super(dictStrategy);
    }

    @Override
    public ChangeRecord getById(Object id) {
        return changeRecordMap.get(id.toString());
    }

    @Override
    public List<String> getDescribe(String id) {
        ChangeRecord changeRecord = changeRecordMap.get(id);
        return ObjUtil.isEmpty(changeRecord) || StrUtil.isBlank(changeRecord.getDescribe()) ? CollUtil.toList() : JSONUtil.toList(changeRecord.getDescribe(), String.class);
    }

    @Override
    public void createChangeDescribeCallBack(ChangeRecord changeRecord, Class<?> returnType) {
        changeRecordMap.put(changeRecord.getId(), changeRecord);
    }

    @Override
    public void createChangeRecordCallBack(ChangeRecord changeRecord, Class<?> returnType) {
        changeRecordMap.put(changeRecord.getId(), changeRecord);
    }


}
